//
//  ImageGalleryView.m
//  NewApp
//
//  Created by  on 12-6-15.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ImageGalleryView.h"
#import "NAImageView.h"

@implementation ImageGalleryView

@synthesize gallerys,delegate,lastImageRect,perferRows;

#pragma mark - 
#pragma mark - Initialization methdos

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.perferRows = 3;
    }
    return self;
}

- (void) dealloc {
    [gallerys release];
    
    [super dealloc];
}

#pragma mark - 
#pragma mark - public methods

- (void) setGalleryWithLargeImages:(NSArray *) largeImages {
    if (nil == largeImages || largeImages.count == 0) 
        return;
    
    self.contentSize = CGSizeMake(largeImages.count * self.frame.size.width,self.frame.size.height);
    self.pagingEnabled = YES;
    
    //Build pages
    for (int i = 0; i < largeImages.count; i++) {
        [self buildGalleryPage:i withImage:[largeImages objectAtIndex:i]];
    }
}

- (void) buildGalleryPage:(int) pageNum withImage:(NAImage *) image {
    NAImageView *imageView = [[NAImageView alloc] initWithFrame:CGRectMake(pageNum * self.frame.size.width, 0,
                                                                       self.frame.size.width, self.frame.size.height)];
    [imageView setNaImage:image];
    [imageView addTarget:self action:@selector(imageViewDidPressed:) 
        forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:imageView];

    UIImage *img = [g_imageLoader cachedImageForURL:image.URL target:imageView];
    if (img != nil) {
        [imageView didLoadImage:image.URL Image:img];
    }

    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(10.f, imageView.frame.size.height - 30.f,
                                                              imageView.frame.size.width - 20.f, 20.f)];
    lblTitle.text = image.title;
    lblTitle.textColor = [UIColor whiteColor];
    lblTitle.backgroundColor = [UIColor clearColor];
    [imageView addSubview:lblTitle];

    [lblTitle release];
    [imageView release];
}

- (void) imageViewDidPressed:(id) sender {
    NAImageView *imageView = (NAImageView *) sender;
    
    NSLog(@"%@",imageView.naImage.title);
    
    if (self.delegate != nil) {
        if ([self.delegate respondsToSelector:@selector(galleryImageDidPressed:)]) {
            [self.delegate galleryImageDidPressed:imageView.naImage];
        }
    }
}


@end
